package lg.minecraft.plugin.library.item.builder;

import lg.minecraft.plugin.library.common.PlaceholderReplacer;
import lg.minecraft.plugin.library.item.ItemProperties;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import java.util.Map;

/**
 * @author langle__
 * @version 1.0
 */
public class PlaceholderItemBuilder extends AbstractItemBuilder {

    public PlaceholderItemBuilder(ItemProperties properties) {
        super(properties);
    }

    @Override
    protected ItemStack build(Map<String, String> map, Object... args) {
        return buildBasicItem(properties, map);
    }

    public ItemStack build(Map<String, String> map) {
        return this.build(map, (Object) null);
    }

    public static ItemStack buildBasicItem(ItemProperties properties, Map<String, String> map) {
        ItemStack stack = new ItemStack(properties.getMaterial());
        ItemMeta meta = stack.getItemMeta();
        if (meta != null) {
            meta.setDisplayName(PlaceholderReplacer.replaceInString(properties.getName(), map));
            meta.setLore(PlaceholderReplacer.replaceInList(properties.getLore(), map));
            properties.getEnchantments().forEach(enchantedEntry -> meta.addEnchant(enchantedEntry.getEnchantment(), enchantedEntry.getLevel(), true));
            properties.getItemFlags().forEach(meta::addItemFlags);
            stack.setItemMeta(meta);
        }
        return stack;
    }

}
